package com.wenhai.spring.bean.lifecycle;

import com.wenhai.xiaomage.spring.ioc.overview.dependency.domain.User;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

/**
 * @Author:wenhai
 * @Description: Bean元信息
 * @Date:2020/3/16
 */
public class XmlBeanMetadataConfigurationDemo {
    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        PropertiesBeanDefinitionReader definitionReader = new PropertiesBeanDefinitionReader(beanFactory);
        beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
        beanFactory.registerSingleton("singletonBean", new SingletonBean());
        String location = "/META-INF/user.properties";
        Resource resource = new ClassPathResource(location);
        EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
        int beanNum = definitionReader.loadBeanDefinitions(encodedResource);
        System.out.println("已加载BeanDefinition的数量" + beanNum);
        User user = beanFactory.getBean("user", User.class);
        SingletonBean singletonBean = beanFactory.getBean("singletonBean", SingletonBean.class);
        System.out.println(user);
        System.out.println(singletonBean);
        beanFactory.destroySingletons();


    }
}
